/*
 * Copyright ©2015-2023 Jaemon. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.cloud.alarm.dinger.config;

import com.cloud.alarm.dinger.multi.MultiDingerAlgorithmInjectRegister;
import com.cloud.alarm.dinger.support.*;
import com.cloud.alarm.dinger.support.sign.ByteTalkSignAlgorithm;
import com.cloud.alarm.dinger.support.sign.DingTalkSignAlgorithm;
import com.cloud.alarm.dinger.support.sign.DingerSignAlgorithm;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static com.cloud.alarm.dinger.constant.DingerConstant.BeanName.*;


/**
 * 实例化bean配置
 *
 * @author shuai.zhou
 * @since 1.0
 */
@Configuration
public class BeanConfiguration {

    /**
     * 默认Text消息格式配置
     */
    @Bean(TEXT_MESSAGE_BEAN_NAME)
    @ConditionalOnMissingBean(name = TEXT_MESSAGE_BEAN_NAME)
    public CustomMessage textMessage() {
        return new TextMessage();
    }

    /**
     * 默认markdown消息格式配置
     */
    @Bean(MARKDOWN_MESSAGE_BEAN_NAME)
    @ConditionalOnMissingBean(name = MARKDOWN_MESSAGE_BEAN_NAME)
    public CustomMessage markDownMessage() {
        return new MarkDownMessage();
    }

    /**
     * 默认interactive消息格式配置
     */
    @Bean(INTERACTIVE_MESSAGE_BEAN_NAME)
    @ConditionalOnMissingBean(name = INTERACTIVE_MESSAGE_BEAN_NAME)
    public CustomMessage interactiveMessage() {
        return new InteractiveMessage();
    }

    /**
     * DingTalk签名算法
     */
    @Bean(DINGTALK_SIGN_ALGORITHM_BEAN_NAME)
    @ConditionalOnMissingBean(name = DINGTALK_SIGN_ALGORITHM_BEAN_NAME)
    public DingerSignAlgorithm dingTalkSignAlgorithm() {
        return new DingTalkSignAlgorithm();
    }

    /**
     * ByteTalk签名算法
     */
    @Bean(BYTETALK_SIGN_ALGORITHM_BEAN_NAME)
    @ConditionalOnMissingBean(name = BYTETALK_SIGN_ALGORITHM_BEAN_NAME)
    public DingerSignAlgorithm byteTalkSignAlgorithm() {
        return new ByteTalkSignAlgorithm();
    }

    /**
     * 默认dkid生成配置
     */
    @Bean(DINGER_ID_GENERATOR_BEAN_NAME)
    @ConditionalOnMissingBean(name = DINGER_ID_GENERATOR_BEAN_NAME)
    public DingerIdGenerator dingerIdGenerator() {
        return new DefaultDingerIdGenerator();
    }

    /**
     * 默认异步执行回调实例
     */
    @Bean(DINGER_ASYNC_CALLBACK_BEAN_NAME)
    @ConditionalOnMissingBean(name = DINGER_ASYNC_CALLBACK_BEAN_NAME)
    public DingerAsyncCallback dingerAsyncCallback() {
        return new DefaultDingerAsyncCallable();
    }

    /**
     * 默认异常回调实例
     */
    @Bean(DINGER_EXCEPTION_CALLBACK_BEAN_NAME)
    @ConditionalOnMissingBean(name = DINGER_EXCEPTION_CALLBACK_BEAN_NAME)
    public DingerExceptionCallback dingerExceptionCallback() {
        return new DefaultDingerExceptionCallback();
    }

    /**
     * 多机器人算法实例
     */
    @Bean
    public static MultiDingerAlgorithmInjectRegister multiDingerAlgorithmInjectRegister() {
        return new MultiDingerAlgorithmInjectRegister();
    }

}